package com.hjjt.baselib.permission

import android.os.Bundle
import android.util.Log
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContract
import androidx.activity.result.contract.ActivityResultContracts
import androidx.fragment.app.Fragment

/**
 *
 * @ProjectName:    IntelligentTerminal
 * @Package:        com.hjjt.baselib.permission
 * @ClassName:      PermissionFragment
 * @Description:
 * @Author:         孙浩
 * @CreateDate:     2022/8/8 8:33
 */
class PermissionFragment private constructor() : Fragment() {

    var callBack: ((map: Map<String, Boolean>) -> Unit)? = null

    val permissionRegister =
        registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) {
            callBack?.invoke(it)
        }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.getStringArray("permissionArray").let { permission ->
            permissionRegister.launch(permission)
        }
    }

    companion object {
        fun newInstance(vararg permissionArray: String): PermissionFragment {
            val fragment = PermissionFragment()
            val arg = Bundle()
            arg.putStringArray("permissionArray", permissionArray)
            fragment.arguments = arg
            return fragment
        }
    }
}